/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 * @lint-ignore-every XPLATJSCOPYRIGHT1
 */
import {
  NavigationActions, StackActions
} from 'react-navigation';
import React, {Component} from 'react';
import {StyleSheet, Text, View, Button, ImageBackground, StatusBar} from 'react-native';



export default class Welcome extends Component{
  constructor(props) {
    super(props);
    this.state = {
      day: 0,
      h: 0,
      m: 0,
      s: 0,
      time: 3
    }
  }
  componentDidMount () {
    this.getDay();
    let tempDay = setInterval(() => {
      this.setState({
        s: this.state.s-1,
        time: this.state.time-1
      })
    }, 1000)
    
    setTimeout(()=> {
      clearInterval(tempDay);
      const resetAction = StackActions.reset({
        index: 0,
        actions: [
          NavigationActions.navigate({routeName: 'TabNav'}),
        ],
      });
      this.props.navigation.dispatch(resetAction)
    }, 3000)
  }

  getDay() {
    let endDay = new Date("2019/12/31 11:59:59").getTime();
    let startDay = new Date().getTime();
    // 还剩天数
    this.setState({
      day: parseInt((endDay - startDay)/(24*60*60*1000)),
      h: parseInt((endDay - startDay)/(60*60*1000)),
      m: parseInt((endDay - startDay)/(60*1000)),
      s: parseInt((endDay - startDay)/(1000))
    })
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.container}>  
        <StatusBar  
         hidden={true}  //是否隐藏状态栏。  
        >  
        </StatusBar>  
      </View>
        <ImageBackground source={require('../../assets/img/start.jpg')} style={{width: '100%', height: '100%'}}>
          <View style={styles.content}>
            <Text style={styles.title}> 距离2020年还剩
            </Text>
            <Text style={styles.item}> {this.state.day}天</Text>
            <Text style={styles.item}> {this.state.h}小时</Text>
            <Text style={styles.item}> {this.state.m}分</Text>
            <Text style={styles.item}> {this.state.s}秒</Text>
            <Text style={styles.dump}>{this.state.time}</Text>
          </View>
        </ImageBackground>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    width: '100%',
    height: '100%',
    backgroundColor: '#F5FCFF',
  },
  content: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  title: {
    color: '#fff',
    fontSize: 20,
    fontWeight: 'bold',
    marginBottom: 10
  },
  item: {
    color: '#fff',
    fontSize: 16,
    marginBottom: 6
  },
  dump: {
    position: 'absolute',
    top: 10,
    right: 10,
    color: '#fff',
    fontSize: 12
  }
});
